﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace CourseManager.View
{
	public partial class CheckCodeImage1 : System.Web.UI.Page
	{
		static string[] FontItems = { "Arial", "Helvetica", "Geneva", "sans-serif", "Verdana" };

		static Brush[] BrushItems = {	Brushes.OliveDrab, 
										Brushes.ForestGreen, 
										Brushes.DarkCyan, 
										Brushes.LightSlateGray, 
										Brushes.RoyalBlue, 
										Brushes.SlateBlue, 
										Brushes.DarkViolet, 
										Brushes.MediumVioletRed, 
										Brushes.IndianRed, 
										Brushes.Firebrick, 
										Brushes.Chocolate, 
										Brushes.Peru, 
										Brushes.Goldenrod }; 

		static string[] BrushName = {	"OliveDrab", 
										"ForestGreen", 
										"DarkCyan", 
										"LightSlateGray", 
										"RoyalBlue", 
										"SlateBlue", 
										"DarkViolet", 
										"MediumVioletRed", 
										"IndianRed", 
										"Firebrick", 
										"Chocolate", 
										"Peru", 
										"Goldenrod"  }; 

		protected void Page_Load(object sender, EventArgs e)
		{
			var checkCode = CreateCheckCodeString();
			Session["CheckCode"] = checkCode.ToLower();
			CreateImages(checkCode);
		}

		public static string CreateCheckCodeString()
		{
			var code = string.Empty;
			var random = new Random();
			for (int i = 0; i < 4; ++i)
			{
				int n = random.Next(62);
				if(n < 10)
				{
					code += n.ToString();
				}
				else if (n < 36)
				{
					code += (char)(n - 10 + 'A');
				}
				else
				{
					code += (char)(n -36 + 'a');
				}
			}
			return code;
		}


		public void CreateImages(string checkCode)
		{
			var image = new Bitmap(60, 22);
			var g = Graphics.FromImage(image);
			try
			{
				var random = new Random();
				g.Clear(Color.White);
				for (int i = 0; i < 25; i++)
				{
					int x1 = random.Next(image.Width);
					int x2 = random.Next(image.Width);
					int y1 = random.Next(image.Height);
					int y2 = random.Next(image.Height);
					g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
				}
				var font = new Font(FontItems[random.Next(FontItems.Length)], 12, (FontStyle.Bold | FontStyle.Italic));
				g.DrawString(checkCode, font, BrushItems[random.Next(BrushItems.Length)], 1, 2);
				for (int i = 0; i < 20; i++)
				{
					int x = random.Next(image.Width);
					int y = random.Next(image.Height);
					image.SetPixel(x, y, Color.FromName(BrushName[random.Next(BrushName.Length)]));
				}
				g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
				var ms = new MemoryStream();
				image.Save(ms, ImageFormat.Gif);
				Response.ClearContent();
				Response.ContentType = "image/Gif";
				Response.BinaryWrite(ms.ToArray());
			}
			finally
			{
				g.Dispose();
				image.Dispose();
			}
		}

		override protected void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}


		private void InitializeComponent()
		{
			Load += Page_Load;
		}
	}
}